
package net.vinote.smart.platform.controller.attendance;

import java.sql.SQLException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.vinote.smart.platform.comm.StringFunction;
import net.vinote.smart.platform.module.database.DBManager;
import net.vinote.smart.platform.module.database.SerialNoManager;
import net.vinote.smart.platform.module.database.bo.BizObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
public class AttendanceController
{
	@RequestMapping(value = "addTodayAttendance.html", method = RequestMethod.GET)
	public String doAttendance(HttpServletRequest request,
			HttpServletResponse response)
	{
		String userName = request.getParameter("userName");
		if (userName != null)
		{
			response.addCookie(new Cookie("userName", userName));
		}
		else
		{
			Cookie[] cookies = request.getCookies();
			if (cookies != null)
			{
				for (Cookie cookie : cookies)
				{
					if ("userName".equals(cookie.getName()))
					{
						userName = cookie.getValue();
						break;
					}
				}
			}
		}

		if (userName == null)
		{
			return "attendance/attendance_login";
		}
		try
		{
			DBManager bm = new DBManager("user_info");
			BizObject bo = bm.createQuery(new String[]
			{
				"USERID"
			}, "userid=?").setParamVal(userName).getSingleResult();
			if (bo == null)
			{
				return "attendance/attendance_login";
			}
			bm = new DBManager("attendance_info");
			BizObject bo1 = new BizObject();
			String serialNo = SerialNoManager.getInstance().getSerialNo(
					"attendance_info");
			bo1.setAttribute("id", serialNo);
			bo1.setAttribute("userId", userName);
			bo1.setAttribute("inputDate", StringFunction.getTime());
			bm.saveBizObject(bo1);
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		return "redirect:/datawindow_page.html?pagePath=service.attendance_list.page";
	}
}
